//
//  RBRegisterCompleteInfoVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/7/5.
//

#import "RBRegisterCompleteInfoVC.h"
#import "RBDefine.h"
#import "RBNavigationBar.h"
#import "UIView+RBAdd.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import <Masonry.h>
#import "RBGradientView.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBLoginRuleView.h"
#import "RBTextField.h"
#import "RBTimerButton.h"
#import "UIScrollView+RBAdd.h"
#import "RBZoneChooseVC.h"
#import "RBSmsResponse.h"
#import "UIViewController+RBAdd.h"
#import "UINavigationController+RBAdd.h"
#import "RBRegisterStateVC.h"

@interface RBRegisterCompleteInfoVC ()

@property(nonatomic, strong)RBGradientView *bgView;
@property(nonatomic, strong)RBNavigationBar *navigationBar;
@property(nonatomic, strong)IBOutlet UIView *topView;
@property(nonatomic, strong)IBOutlet RBTextField *nameF;
@property(nonatomic, strong)IBOutlet RBTextField *careerF;
@property(nonatomic, strong)IBOutlet RBTextField *phoneF;
@property(nonatomic, strong)IBOutlet RBTextField *codeF;
@property(nonatomic, strong)IBOutlet RBTimerButton *timerBtn;
@property(nonatomic, strong)IBOutlet RBTextField *orgNameF;
@property(nonatomic, strong)IBOutlet UILabel *provinceCityL;
@property(nonatomic, strong)IBOutlet RBTextField *detailAddressF;
@property(nonatomic, strong)UIScrollView *scrollView;

@property(nonatomic, copy) NSString *sms_id;
@property(nonatomic, copy) NSString *province;
@property(nonatomic, copy) NSString *city;
@property(nonatomic, copy) NSString *zone;

@end

@implementation RBRegisterCompleteInfoVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = UIColor.whiteColor;
    self.bgView.hidden = NO;
    self.navigationBar.hidden = NO;
    __weak typeof(self) weakSelf = self;
    self.phoneF.type = RBTextFieldTypePhone;
    self.timerBtn.timerKey = NSStringFromClass(self.class);
    self.timerBtn.customSettingBlock = ^(RBTimerButton *btn, NSTimeInterval cdF, NSInteger cdI, BOOL countingDown) {
        if (countingDown)
        {
            [btn setTitle:[NSString stringWithFormat:@"%zdS后重发", cdI] forState:UIControlStateNormal];
        }
        else
        {
            [btn setTitle:@"获取验证码" forState:UIControlStateNormal];
        }
    };
    [self.timerBtn config];
    self.codeF.type = RBTextFieldTypePhone;
    self.codeF.maxTextLength = 8;
    
    [self.topView zoomSubViewsWithSelf];
    self.topView.height = Zoom(self.topView.height);
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    [self.scrollView addView:self.topView withHeight:self.topView.height width:kRBMainScreenWidth top:Zoom(50) bottom:MAX(Zoom(20), kRBSafeBottomHeight) left:0 right:0 centerX:0];
    
    if(self.detailResponse)
    {
        self.nameF.text = self.detailResponse.data.name;
        self.careerF.text = self.detailResponse.data.job_name;
        self.phoneF.text = self.detailResponse.data.mobile;
        self.orgNameF.text = self.detailResponse.data.company_name;
        self.province = self.detailResponse.data.province;
        self.city = self.detailResponse.data.city;
        self.zone = self.detailResponse.data.district;
        self.provinceCityL.text = [NSString stringWithFormat:@"%@ %@ %@", self.province, self.city, self.zone];
        self.detailAddressF.text = self.detailResponse.data.addr_detail;
    }
}

#pragma mark - Network

#pragma mark - Event

- (IBAction)timerBtnTapped:(id)sender
{
    if(!self.phoneF.validate)
    {
        [self.view showTip:@"请输入正确手机号" completion: nil];
        return;
    }
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"mobile"] = self.phoneF.text;
    } path:@"han_getsmscode" post:YES log:YES responseClass:RBSmsResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBSmsResponse *data) {
        weak_self.sms_id = data.data.sms_id;
        [weak_self.timerBtn startTimer];
    } failBlock:^(RBResponse *data) {
        [weak_self.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

- (IBAction)provinceCityBtnTapped:(id)sender
{
    __weak typeof(self) weak_self = self;
    RBZoneChooseVC *vc = RBZoneChooseVC.new;
    vc.block = ^(NSString * _Nonnull province, NSString * _Nonnull city, NSString * _Nonnull zone) {
        weak_self.province = province;
        weak_self.city = city;
        weak_self.zone = zone;
        weak_self.provinceCityL.text = [NSString stringWithFormat:@"%@ %@ %@", weak_self.province, weak_self.city, weak_self.zone];
    };
    [self presentVc:vc];
}

- (IBAction)confirmBtnTapped:(id)sender
{
    if(!self.nameF.text.length)
    {
        [self.view showTip:self.nameF.placeholder completion:nil];
        return;
    }
    if(self.phoneF.text.length)
    {
        if(!self.phoneF.validate)
        {
            [self.view showTip:@"请输入正确手机号" completion:nil];
            return;
        }
        else
        {
            if(!self.sms_id.length)
            {
                [self.view showTip:@"请获取验证码" completion:nil];
                return;
            }
            if(!self.codeF.text.length)
            {
                [self.view showTip:@"请输入验证码" completion:nil];
                return;
            }
        }
    }
    if(!self.orgNameF.text.length)
    {
        [self.view showTip:self.orgNameF.placeholder completion:nil];
        return;
    }
    if(!self.province.length)
    {
        [self.view showTip:@"请选择省市区" completion:nil];
        return;
    }
    if(!self.detailAddressF.text.length)
    {
        [self.view showTip:self.detailAddressF.placeholder completion:nil];
        return;
    }
    __weak typeof(self) weak_self = self;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"name"] = self.nameF.text;
        dicM[@"job_name"] = self.careerF.text;
        dicM[@"mobile"] = self.phoneF.text;
        dicM[@"sms_code"] = self.codeF.text;
        dicM[@"sms_id"] = self.sms_id;
        dicM[@"company_name"] = self.orgNameF.text;
        dicM[@"province"] = self.province;
        dicM[@"addr_detail"] = self.detailAddressF.text;
        dicM[@"user_id"] = self.loginResponse.data.user_id;
        dicM[@"city"] = self.city;
        dicM[@"district"] = self.zone;
    } path:@"han_user_detail" post:YES log:YES responseClass:RBResponse.class desView:self.view showHud:YES showPlaceHolder:NO sucBlock:^(RBResponse *data) {
        weak_self.loginResponse.data.step = 2;
        RBRegisterStateVC *vc = RBRegisterStateVC.new;
        vc.loginResponse = weak_self.loginResponse;
        [weak_self.navigationController pushToViewController:vc animated:YES removeVcCount:1];
    } failBlock:^(RBResponse *data) {
        [weak_self.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Setter

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = UIScrollView.new;
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                make.top.mas_equalTo(kRBStatusBarHeight + kRBNavigationBarHeight);
            }];
            view;
        });
    }
    return _scrollView;
}

- (RBGradientView *)bgView
{
    if (!_bgView)
    {
        self.bgView = ({
            RBGradientView *view = [RBGradientView gradientViewWithColors:@[[UIColor colorWithHexString:@"#CADCFF"], [UIColor colorWithHexString:@"#ffffff"]] locations:@[@0, @1] vertical:YES];
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.right.mas_equalTo(0);
                make.height.mas_equalTo(595);
            }];
            view;
        });
    }
    return _bgView;
}

- (RBNavigationBar *)navigationBar
{
    if (!_navigationBar)
    {
        self.navigationBar = ({
            RBNavigationBar *bar = RBNavigationBar.new;
            bar.needGradientView = NO;
            [self.view addSubview:bar];
            [bar mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.right.mas_equalTo(0);
                make.height.mas_equalTo(kRBStatusBarHeight + kRBNavigationBarHeight);
            }];
            [bar.naviBar addBackBtnWithWidth:70 image:[UIImage imageNamed:@"backBlack"]];
            bar;
        });
    }
    return _navigationBar;
}

@end
